<?php

/*
 *
 * sysmanage.Area  地区管理登录   
 *
 * =========================================================
 * 零起飞网络 - 专注于网站建设服务和行业系统开发
 * 以质量求生存，以服务谋发展，以信誉创品牌 !
 * ----------------------------------------------
 * @copyright	Copyright (C) 2017-2018 07FLY Network Technology Co,LTD (www.07FLY.com) All rights reserved.
 * @license    For licensing, see LICENSE.html or http://www.07fly.xyz/crm/license
 * @author ：kfrs <goodkfrs@QQ.com> 574249366
 * @version ：1.0
 * @link ：http://www.07fly.xyz 
 */

class Area extends Action
{
    private $cacheDir = '';//缓存目录
    private $tree = '';//数形
    public function area()
    {
        $sql = "select * from fly_sys_area  where visible='1' order by sort asc;";
        $list = $this->C($this->cacheDir)->findAll($sql);
        return $list;
    }

    public function area_json() {
        //**获得传送来的数据作分页处理
        $pageNum = $this->_REQUEST("pageNum");//第几页
        $pageSize= $this->_REQUEST("pageSize");//每页多少条
        $pageNum = empty($pageNum)?1:$pageNum;
        $pageSize= empty($pageSize)?$GLOBALS["pageSize"]:$pageSize;
        //**************************************************************************

        //**获得传送来的数据做条件来查询
        $keywords  = $this->_REQUEST("keywords");
        $pid   	        = $this->_REQUEST("pid");
        $pid_son=$this->get_area_self_son($pid);
        $pid_txt=implode(",",$pid_son);

        $where_str 	   = " id>'0' ";
        if( !empty($keywords) ){
            $where_str .=" and name like '%$keywords%'";
        }
        if( !empty($pid) ){
            $where_str .=" and parentID in ($pid_txt)";
        }
        $countSql    = "select *  from fly_sys_area where  $where_str order by sort asc;";
        $totalCount  = $this->C($this->cacheDir)->countRecords($countSql);	//计算记录数
        $beginRecord= ($pageNum-1)*$pageSize;//计算开始行数

        $sql		 = "SELECT *  FROM fly_sys_area WHERE  $where_str  order by sort asc limit $beginRecord,$pageSize";
        $list		 = $this->C($this->cacheDir)->findAll($sql);
        $assignArray = array('list'=>$list,"pageSize"=>$pageSize,"totalCount"=>$totalCount,"pageNum"=>$pageNum);
        echo json_encode($assignArray);
    }

    public function area_tree_json() {
        $list=$this->area();
        $tree=list2tree($list,0,0,'id','parentID','name');
        echo json_encode($tree);
    }


    public function area_show()
    {
        $smarty = $this->setSmarty();
        $smarty->display('sysmanage/area_show.html');
    }

    public function area_add()
    {
        if (empty($_POST)) {
            $pid = $this->_REQUEST('pid');
            $area_list = $this->area_select_tree();
            $smarty = $this->setSmarty();
            $smarty->assign(array("area_list" => $area_list,"pid" => $pid));//框架变量注入同样适用于smarty的assign方法
            $smarty->display('sysmanage/area_add.html');
        } else {
            $name = $this->_REQUEST("name");
            $parentID = $this->_REQUEST("parentID");
            $sort = $this->_REQUEST("sort");
            $visible = $this->_REQUEST("visible");
            $intro = $this->_REQUEST("intro");
            $data = array(
                'name' => $name,
                'parentID' => $parentID,
                'sort' => $sort,
                'visible' => $visible,
                'intro' => $intro
            );
            $this->C($this->cacheDir)->insert('fly_sys_area', $data);
            $this->L("Common")->ajax_json_success("操作成功");
        }
    }

    public function area_modify()
    {
        $id = $this->_REQUEST("id");
        if (empty($_POST)) {
            $sql = "select * from fly_sys_area where id='$id'";
            $one = $this->C($this->cacheDir)->findOne($sql);
            $area_list = $this->area_select_tree();
            $smarty = $this->setSmarty();
            $smarty->assign(array("one" => $one, "area_list" => $area_list));
            $smarty->display('sysmanage/area_modify.html');
        } else {
            $name = $this->_REQUEST("name");
            $parentID = $this->_REQUEST("parentID");
            $sort = $this->_REQUEST("sort");
            $visible = $this->_REQUEST("visible");
            $intro = $this->_REQUEST("intro");
            $pid=$this->_REQUEST( "parentID" );
            if($pid==$id){
                $this->L("Common")->ajax_json_error("父级栏目不能选择自己");
                return false;
                exit;
            }
            $data = array(
                'name' => $name,
                'parentID' => $parentID,
                'sort' => $sort,
                'visible' => $visible,
                'intro' => $intro
            );
            $this->C($this->cacheDir)->modify('fly_sys_area', $data, "id='$id'");
            $this->L("Common")->ajax_json_success("操作成功");
        }
    }

    public function area_del()
    {
        $id = $this->_REQUEST("id");
        $sql = "delete from fly_sys_area where id='$id'";
        $this->C($this->cacheDir)->update($sql);
        $this->L("Common")->ajax_json_success("操作成功");
    }


    /**树形下拉
     * @return array|string
     * Author: lingqifei created by at 2020/4/3 0003
     */
    public function area_select_tree()
    {
        $sql = "select * from fly_sys_area order by sort asc;";
        $list = $this->C($this->cacheDir)->findAll($sql);
        $listselect=list2select($list,0,0,'id','parentID','name');
        return $listselect;
    }

    //传入ID返回名字
    public function area_get_name($id)
    {
        if (empty($id)) $id = 0;
        $sql = "select id,name from fly_sys_area where id in ($id)";
        $list = $this->C($this->cacheDir)->findAll($sql);
        $str = "";
        if (is_array($list)) {
            foreach ($list as $row) {
                $str .= "|-" . $row["name"] . "&nbsp;";
            }
        }
        return $str;
    }

    //排序
    public function area_modify_sort()
    {
        $id = $this->_REQUEST('id');
        $sort = $this->_REQUEST('sort');
        $upt_data = array(
            'sort' => $this->_REQUEST("sort")
        );
        $this->C($this->cacheDir)->modify('fly_sys_area', $upt_data, "id='$id'", true);
        $this->L("Common")->ajax_json_success("操作成功");
    }

    //修改名称
    public function area_modify_name()
    {
        $id = $this->_REQUEST('id');
        $name = $this->_REQUEST('name');
        $upt_data = array(
            'name' => $this->_REQUEST("name")
        );
        $this->C($this->cacheDir)->modify('fly_sys_area', $upt_data, "id='$id'", true);
        $this->L("Common")->ajax_json_success("操作成功");
    }

    /**获得所有指定id所有父级
     * @param int $areaid
     * @param array $data
     * @return array
     */
    public function get_area_all_pid($areaid=0, $data=[])
    {
        $sql	= "select *  from fly_sys_area where parentID='$areaid' order by sort asc;";
        $info = $this->C( $this->cacheDir )->findOne( $sql );
        if(!empty($info) && $info['parentID']){
            $data[]=$info['parentID'];
            return $this->get_area_all_pid($info['parentID'],$data);
        }
        return $data;
    }

    /**获得所有指定id所有子级
     * @param int $areaid
     * @param array $data
     * @return array
     */
    public function get_area_all_son($areaid=0, $data=[])
    {

        $sql	= "select *  from fly_sys_area where parentID='$areaid' order by sort asc;";
        $sons = $this->C( $this->cacheDir )->findAll( $sql );
        if (count($sons) > 0) {
            foreach ($sons as $v) {
                $data[] = $v['id'];
                $data = $this->get_area_all_son($v['id'], $data); //注意写$data 返回给上级
            }
        }
        if (count($data) > 0) {
            return $data;
        } else {
            return false;
        }
        return $data;
    }
    /**得到自己的和子级
     * @param $id
     * @return array
     * Author: lingqifei created by at 2020/4/1 0001
     */
    public  function get_area_self_son($id){
        $sons=$this->get_area_all_son($id);
        $sons[]=$id;
        return $sons;
    }

}//
?>